package cn.easier.activity.console.controller;

import cn.easier.activity.console.configration.ActivityConsoleConfiguration;
import cn.easier.activity.console.service.ActivityTemplateInfoService;
import cn.easier.activity.console.utils.FileUtil;
import cn.easier.activity.web.entity.ActivityTemplateInfo;
import cn.easier.club.base.dto.ResultInfo;
import com.google.common.base.Strings;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.File;

/**
 * 模板信息Controller
 */
@Controller
@RequestMapping("/activity/template/info")
public class ActivityTemplateController {

    Logger logger = LoggerFactory.getLogger(ActivityTemplateController.class);

    @Autowired
    ActivityConsoleConfiguration activityConsoleConfiguration;

    @Autowired
    ActivityTemplateInfoService activityTemplateInfoService;

    @GetMapping("/all")
    @ResponseBody
    public ResultInfo getAll() {
        return new ResultInfo(String.valueOf(HttpServletResponse.SC_OK), "SUCCESS", activityTemplateInfoService.findAll());
    }

    /**
     * 创建模板对象
     *
     * @param activityTemplateInfo 模板对象
     * @return
     */
    @PostMapping
    @ResponseBody
    public ResultInfo create(ActivityTemplateInfo activityTemplateInfo, @RequestParam(value = "imageFile", required = false)
            MultipartFile imageFile) {
        ResultInfo resultInfo = null;
        try {
            if (imageFile != null && (!Strings.isNullOrEmpty(imageFile.getOriginalFilename()))) {
                String fileName = imageFile.getOriginalFilename();
                //验证图片文件格式
                boolean imageFormatFlag = FileUtil.imageFormatVerification(fileName);
                if (!imageFormatFlag) {
                    return new ResultInfo(String.valueOf(HttpServletResponse.SC_INTERNAL_SERVER_ERROR), "请选择格式为*.jpg、*.gif、*.bmp、*.png、*.jpeg 的图片", fileName);
                }
                activityTemplateInfo.setImage(uploadFile(imageFile));
            }
            ActivityTemplateInfo returnActivityTemplateInfo = activityTemplateInfoService.save(activityTemplateInfo);
            logger.info("创建模板成功: =>{}", returnActivityTemplateInfo);
            resultInfo = new ResultInfo(String.valueOf(HttpServletResponse.SC_OK), "SUCCESS", returnActivityTemplateInfo);
        } catch (Exception e) {
            e.printStackTrace();
            resultInfo = new ResultInfo(String.valueOf(HttpServletResponse.SC_INTERNAL_SERVER_ERROR), "活动模板创建异常", null);
            logger.error("活动模板创建异常,activityTemplateInfo: =>{}", activityTemplateInfo);
        }
        return resultInfo;
    }

    /**
     * 修改模板对象
     *
     * @param activityTemplateInfo 模板对象
     * @return
     */
    @PutMapping
    @ResponseBody
    public ResultInfo update(ActivityTemplateInfo activityTemplateInfo, @RequestParam(value = "imageFile", required = false)
            MultipartFile imageFile) {
        ResultInfo resultInfo = null;
        try {
            if (imageFile != null && (!Strings.isNullOrEmpty(imageFile.getOriginalFilename()))) {
                String fileName = imageFile.getOriginalFilename();
                //验证图片文件格式
                boolean imageFormatFlag = FileUtil.imageFormatVerification(fileName);
                if (!imageFormatFlag) {
                    return new ResultInfo(String.valueOf(HttpServletResponse.SC_INTERNAL_SERVER_ERROR), "请选择格式为*.jpg、*.gif、*.bmp、*.png、*.jpeg 的图片", fileName);
                }
                activityTemplateInfo.setImage(uploadFile(imageFile));
            }
            ActivityTemplateInfo returnActivityTemplateInfo = activityTemplateInfoService.update(activityTemplateInfo);
            logger.info("更新模板成功: =>{}", returnActivityTemplateInfo);
            resultInfo = new ResultInfo(String.valueOf(HttpServletResponse.SC_OK), "SUCCESS", returnActivityTemplateInfo);
        } catch (Exception e) {
            e.printStackTrace();
            resultInfo = new ResultInfo(String.valueOf(HttpServletResponse.SC_INTERNAL_SERVER_ERROR), "活动模板修改异常", null);
            logger.error("活动模板修改异常,activityTemplateInfo: =>{}", activityTemplateInfo);
        }
        return resultInfo;
    }

    /**
     * 查找模板对象
     *
     * @param id 模板对象编号
     * @return
     */
    @GetMapping(value = "/id/{id}")
    @ResponseBody
    public ResultInfo findOne(@PathVariable Integer id) {
        ResultInfo resultInfo = null;
        try {
            ActivityTemplateInfo returnActivityTemplateInfo = activityTemplateInfoService.findOne(id);
            logger.info("获取模板: =>{}", returnActivityTemplateInfo);
            resultInfo = new ResultInfo(String.valueOf(HttpServletResponse.SC_OK), "SUCCESS", returnActivityTemplateInfo);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("活动模板获取异常,activityTemplateInfoId: =>{}", id);
        }
        return resultInfo;
    }

    /**
     * 查找模板对象
     *
     * @param id 模板对象编号
     * @return
     */
    @GetMapping(value = "/id/{id}/type")
    @ResponseBody
    public String findType(@PathVariable Integer id) {
        return activityTemplateInfoService.getActivityType(id);
    }

    /**
     * 删除模板对象
     *
     * @param id 模板对象编号
     * @return
     */
    @DeleteMapping(value = "/id/{id}")
    @ResponseBody
    public ResultInfo delete(@PathVariable Integer id) {
        ResultInfo resultInfo = null;
        try {
            Boolean deleteFlag = activityTemplateInfoService.delete(id);
            logger.info("删除模板: =>{}", deleteFlag ? "成功" : "失败");
            if (deleteFlag) {
                resultInfo = new ResultInfo(String.valueOf(HttpServletResponse.SC_OK), "SUCCESS", null);
            } else {
                resultInfo = new ResultInfo(String.valueOf(HttpServletResponse.SC_OK), "FAIL", null);
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("活动模板获取异常,activityTemplateInfoId: =>{}", id);
        }
        return resultInfo;
    }

    private String uploadFile(MultipartFile imageFile) {
        //根路径
        String realPath = activityConsoleConfiguration.realPath;
        //文件路径
        String templatePath = activityConsoleConfiguration.activityTemplateImagePath;
        String path = (realPath + templatePath).replace("/", File.separator);
        String imageFileName = FileUtil.doFile(imageFile, path, "activity_template_image_");
        //静态资源映射 + 文件地址 + 上传文件名称
        return templatePath + imageFileName;
    }
}
